package com.yanqu.road.dao.impl.activity.hanghai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hanghai.HangHaiUnionData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HangHaiUnionDataDaoImpl extends TempDataObjectDao<HangHaiUnionData> {

    public static String getHangHaiUnionDataMapSql = "select * from t_c_activity_hanghai_union_data where activityId=?";

    public Map<String, HangHaiUnionData> getHangHaiUnionDataMap(int activityId) {
        Map<String, HangHaiUnionData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHangHaiUnionDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HangHaiUnionData hangHaiUnionData = parse(rs);
                map.put(hangHaiUnionData.getUnionUid(), hangHaiUnionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHangHaiUnionDataSql = "update t_c_activity_hanghai_union_data set lastResetTime=? where activityId=? and unionUid=?";

    @Override
    protected boolean updateData(HangHaiUnionData hangHaiUnionData) {
        hangHaiUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUnionData.getLastResetTime()));
//        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUnionData.getLockMemberMap())));
//        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUnionData.getMemberList())));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiUnionData.getUnionUid()));
        if (getDb().executeNoneQuery(updateHangHaiUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHangHaiUnionDataSql = "insert into t_c_activity_hanghai_union_data(activityId,unionUid,lastResetTime) values (?,?,?)";

    @Override
    protected boolean addData(HangHaiUnionData hangHaiUnionData) {
        hangHaiUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUnionData.getLastResetTime()));
//        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUnionData.getLockMemberMap())));
//        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUnionData.getMemberList())));
        if (getDb().executeNoneQuery(insertHangHaiUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HangHaiUnionData parseData(ResultSet rs) throws SQLException {
        HangHaiUnionData hangHaiUnionData = new HangHaiUnionData();
        hangHaiUnionData.setActivityId(rs.getInt("activityId"));
        hangHaiUnionData.setUnionUid(rs.getString("unionUid"));
        hangHaiUnionData.setLastResetTime(rs.getLong("lastResetTime"));
//        String lockMemberMap = rs.getString("lockMemberMap");
//        if (!StringUtils.isNullOrEmpty(lockMemberMap)) {
//            hangHaiUnionData.setLockMemberMap(JSON.parseObject(lockMemberMap, new TypeReference<ConcurrentHashMap<Long, String>>() {
//            }));
//        }
//        String memberList = rs.getString("memberList");
//        if (!StringUtils.isNullOrEmpty(memberList)) {
//            hangHaiUnionData.setMemberList(JSON.parseObject(memberList, new TypeReference<HashSet<Long>>() {
//            }));
//        }
        return hangHaiUnionData;
    }

}
